﻿#region Usings

using System.IO;
using System.Xml;
using System.Xml.XPath;
using Yandex.Market.Api.Model;

#endregion

namespace Yandex.Market.Api
{
    public class YmlCatalogXmlReader
    {
        public static YmlCatalog CreateYmlCatalogFromXml(Stream xmlStream)
        {
            using(XmlReader xmlTextReader = XmlReader.Create(xmlStream, GetXmlReaderSettings()))
                return CreateYmlCatalogFromXml(xmlTextReader);
        }

        public static YmlCatalog CreateYmlCatalogFromXml(string uri)
        {
            using(XmlReader xmlTextReader = XmlReader.Create(uri, GetXmlReaderSettings()))
                return CreateYmlCatalogFromXml(xmlTextReader);
        }

        private static YmlCatalog CreateYmlCatalogFromXml(XmlReader reader)
        {
            IXPathNavigable doc = new XPathDocument(reader);
            return YmlCatalog.CreateYmlCatalogFromXml(doc);
        }

        private static XmlReaderSettings GetXmlReaderSettings()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.CheckCharacters = false;
            settings.DtdProcessing = DtdProcessing.Ignore;

            return settings;
        }
    }
}